// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.1

package role

import (
	"audio-server/internal/model"
	"audio-server/internal/sqlutil"
	"context"
	"errors"

	"audio-server/internal/svc"
	"audio-server/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RoleDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRoleDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RoleDetailLogic {
	return &RoleDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RoleDetailLogic) RoleDetail(req *types.RoleDetailReq) (resp *types.RoleRes, err error) {
	id := req.RoleId
	roleModel := model.NewSysRoleModel(l.svcCtx.Conn)
	role, err := roleModel.FindOne(l.ctx, int64(id))

	if err != nil {
		return nil, errors.New("角色不存在")
	}
	return &types.RoleRes{
		Name:        role.Name,
		Description: sqlutil.NullStringToString(role.Description),
		RoleId:      int(role.RoleId),
		IsAdmin:     int(role.IsAdmin),
	}, nil

}
